/**
 * Created by IAN on 2016/10/9.
 */
var express = require('express');
var router = express.Router();
var co = require('co');
var seqModule = require('./createModules');
var seq = new seqModule();

//添加
router.get('/addProvince', function(req, res, next) {
    co(function () {
        seq.provinceModule.create(req.query).then(function(results){
            res.json({"success":true,"rows":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'添加失败'});
        console.log(e);
    })
});

//删除
router.get('/deleteProvince', function(req, res, next) {
    co(function () {
        var param_id = req.query.id;
        // var params=req.query.params;
        // var arr=params.split(",");
        // var paramT=[arr];
        seq.provinceModule.destroy({'where':{'id':param_id}}).then(function(results){
            res.json({"success":true,"msg":"删除成功","rows":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'删除失败'});
        console.log(e);
    })
});

//查询
router.get('/getProvinces', function(req, res, next) {
    co(function () {
        var params={};
        var paramT={};
        var paramU={};

        if(req.query.id != undefined){
            params["id"]=req.query.id;
        }
        //模糊查询
        if(req.query.provinceName != undefined){
            paramU["$like"]="%"+req.query.provinceName+"%";
            params["provinceName"]=paramU;
        }
        paramT["where"]=params;
        seq.provinceModule.findAll(paramT).then(function(result){
            res.json({"success":true,"msg":"查询成功","result":result});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});

//修改
router.get('/updateProvince', function(req, res, next) {
    co(function () {
        var params={};
        var paramT={};
        var paramU={'id':req.query.id};
        if(req.query.provinceName != undefined){
            params["provinceName"]=req.query.provinceName;
        }
        paramT["where"]=paramU;
        seq.provinceModule.update(params,paramT
        ).then(function(results){
            res.json({"success":true,"msg":"修改成功","rows":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'修改失败'});
        console.log(e);
    })
});

module.exports = router;